<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>07.Python数据容器 | 王扣笔记</title><meta name="author" content="王扣"><meta name="copyright" content="王扣"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python入门课程  数据容器 章节目录 [!WARNING|label:本章需要学习的内容]   数据容器入门 数据容器：list(列表) list（列表）的遍历 数据容器：tuple(元组) 数据容器：str(字符串) 数据容器的切片 数据容器：set(集合) 数据容器：dict(字典、映射) 数据容器的通用操作 综合案例  数据容器入门 数据容器：list(列表) [!tip|style:">
<meta property="og:type" content="article">
<meta property="og:title" content="07.Python数据容器">
<meta property="og:url" content="http://grace.666443.xyz/posts/24e420f7.html">
<meta property="og:site_name" content="王扣笔记">
<meta property="og:description" content="Python入门课程  数据容器 章节目录 [!WARNING|label:本章需要学习的内容]   数据容器入门 数据容器：list(列表) list（列表）的遍历 数据容器：tuple(元组) 数据容器：str(字符串) 数据容器的切片 数据容器：set(集合) 数据容器：dict(字典、映射) 数据容器的通用操作 综合案例  数据容器入门 数据容器：list(列表) [!tip|style:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/f284fb2dc534d3975a7733ab0eb843eb.png">
<meta property="article:published_time" content="2022-12-08T11:51:23.000Z">
<meta property="article:modified_time" content="2022-12-08T12:08:55.186Z">
<meta property="article:author" content="王扣">
<meta property="article:tag" content="笔记 开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/f284fb2dc534d3975a7733ab0eb843eb.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://grace.666443.xyz/posts/24e420f7"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '07.Python数据容器',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-08 20:08:55'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/f284fb2dc534d3975a7733ab0eb843eb.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王扣笔记</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">07.Python数据容器</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-12-08T11:51:23.000Z" title="发表于 2022-12-08 19:51:23">2022-12-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-12-08T12:08:55.186Z" title="更新于 2022-12-08 20:08:55">2022-12-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python%E5%9F%BA%E7%A1%80/">python基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="07.Python数据容器"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python入门课程-数据容器"><a href="#Python入门课程-数据容器" class="headerlink" title="Python入门课程  数据容器"></a>Python入门课程  数据容器</h1><hr>
<h2 id="章节目录"><a href="#章节目录" class="headerlink" title="章节目录"></a><strong>章节目录</strong></h2><blockquote>
<p>[!WARNING|label:本章需要学习的内容]</p>
</blockquote>
<ul>
<li>数据容器入门</li>
<li>数据容器：list(列表)</li>
<li>list（列表）的遍历</li>
<li>数据容器：tuple(元组)</li>
<li>数据容器：str(字符串)</li>
<li>数据容器的切片</li>
<li>数据容器：set(集合)</li>
<li>数据容器：dict(字典、映射)</li>
<li>数据容器的通用操作</li>
<li>综合案例</li>
</ul>
<h2 id="数据容器入门"><a href="#数据容器入门" class="headerlink" title="数据容器入门"></a><strong>数据容器入门</strong></h2><p><img src="https://img-blog.csdnimg.cn/c62ff34c92f744af9f8495218fb49363.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/16a0a667ca1249bab435c1537f47e410.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/9e66a341445d4f31bd0642db46e993bc.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/b91286b52f06422897d436a8e398e493.png" alt="4.jpg"></p>
<h2 id="数据容器：list-列表"><a href="#数据容器：list-列表" class="headerlink" title="数据容器：list(列表)"></a><strong>数据容器：list(列表)</strong></h2><blockquote>
<p>[!tip|style:callout|label:列表] ①列表的定义<br>②列表的下标索引<br>③列表的常用操作</p>
</blockquote>
<h3 id="①列表的定义"><a href="#①列表的定义" class="headerlink" title="①列表的定义"></a>①列表的定义</h3><p><img src="https://img-blog.csdnimg.cn/fa9ae66ec3794a38880626393bdd61e6.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/ca762ff616c44c05b64038698e908982.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/518ffd3c9b364bb29fe9fdaaae65eadb.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/8692303f057249b5a1875f3fce5d791f.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/0314b44ba0a84065aedc528f882a5777.png" alt="5.jpg"></p>
<h3 id="②列表的下标索引"><a href="#②列表的下标索引" class="headerlink" title="②列表的下标索引"></a>②列表的下标索引</h3><p><img src="https://img-blog.csdnimg.cn/8ee701d4f52d44b2adff392f94efa5dd.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/5b4a2533fc584fe6801e464b16490dd8.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/08e459dc3a3f4c948a8053a3e1d4efdc.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/6186279149ae4d68906f7f69baf0ebde.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/f11e543d718947d3baf3a890eee05cb4.png" alt="5.jpg"></p>
<h3 id="③列表的常用操作"><a href="#③列表的常用操作" class="headerlink" title="③列表的常用操作"></a>③列表的常用操作</h3><blockquote>
<p><strong>学习目标：掌握列表的常用操作（方法）和特点</strong></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/615e61a5d26c4b3ea9c4c7fff73f22d8.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/3ad6c8d3672348ceaed3bea879da28ba.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/a7b49f092c044a609d61d95a083c2fc3.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/47b5d85f3def490b9c40fea6ddad9d6c.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/d3b0a93bef524e89bb74083830ffed0b.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/9aa7967660d74bd487711f933928c168.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/52c23be43969459f953adf150d20fae3.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/26fb8b1f977049428396bbb3dfdd7a0d.png" alt="9.jpg"><br><img src="https://img-blog.csdnimg.cn/c7343cf4ddd643f3a8c141b9d2a44fed.png" alt="10.jpg"><br><img src="https://img-blog.csdnimg.cn/0d38af404dce41c7983733144bb71f4f.png" alt="11.jpg"><br><img src="https://img-blog.csdnimg.cn/0f541acf90994e75ae8904ca9aff623f.png" alt="12.jpg"></p>
<blockquote>
<p>代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line"><span class="comment"># 查询某元素所在的下标</span></span><br><span class="line">index = my_list.index(<span class="number">1</span>)</span><br><span class="line"><span class="built_in">print</span>(index)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改某索引的值</span></span><br><span class="line"></span><br><span class="line">my_list[<span class="number">2</span>] = <span class="string">&#x27;java&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(my_list)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 插入元素</span></span><br><span class="line">my_list.insert(<span class="number">1</span>, <span class="string">&#x27;php&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 追加元素</span></span><br><span class="line">my_list.append(<span class="string">&#x27;html&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除元素</span></span><br><span class="line"><span class="comment">#del my_list[0]</span></span><br><span class="line"><span class="comment">#print(my_list)</span></span><br><span class="line"></span><br><span class="line">my_list.pop(<span class="number">0</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list) </span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/5956d95287ce4141ab810a3e562a65b1.png" alt="13.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"> 练习案例:常用功能练习</span></span><br><span class="line"><span class="string">有一个列表，内容是: [21,25, 21, 23, 22, 20]，记录的是- -批学生的年龄</span></span><br><span class="line"><span class="string">请通过列表的功能(方法)，对其进行</span></span><br><span class="line"><span class="string">1.定义这个列表,并用变量接收它</span></span><br><span class="line"><span class="string">2.追加一 个数字31，到列表的尾部</span></span><br><span class="line"><span class="string">3.追加一个新列表[29, 33, 30],到列表的尾部</span></span><br><span class="line"><span class="string">4.取出第一 个元素(应是: 21)</span></span><br><span class="line"><span class="string">5.取出最后-  个元素(应是: 30)</span></span><br><span class="line"><span class="string">6.查找元素31, 在列表中的下标位置</span></span><br><span class="line"><span class="string"> &quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># -批学生的年龄</span></span><br><span class="line">ages = [<span class="number">21</span>,<span class="number">25</span>, <span class="number">21</span>, <span class="number">23</span>, <span class="number">22</span>, <span class="number">20</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 追加一 个数字31，到列表的尾部</span></span><br><span class="line">ages.append(<span class="number">31</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 追加一个新列表[29, 33, 30],到列表的尾部</span></span><br><span class="line">ages.extend([<span class="number">29</span>, <span class="number">33</span>, <span class="number">30</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 取出第一 个元素(应是: 21)</span></span><br><span class="line">num1 = ages[<span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;从列表中取出的第一个元素 <span class="subst">&#123;num1&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 取出最后-  个元素(应是: 30)</span></span><br><span class="line">num2 = ages[-<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;取出最后一个元素 <span class="subst">&#123;num2&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查找元素31, 在列表中的下标位置</span></span><br><span class="line">index = ages.index(<span class="number">31</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;元素31 在列表中下标为:<span class="subst">&#123;index&#125;</span>&quot;</span>) </span><br></pre></td></tr></table></figure>
<h2 id="list（列表）的遍历"><a href="#list（列表）的遍历" class="headerlink" title="list（列表）的遍历"></a><strong>list（列表）的遍历</strong></h2><p><img src="https://img-blog.csdnimg.cn/096f1b664bcd47df82af6c160f933451.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/c272ba18cb4f4c4bb6b4dd19c1067e04.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/90c99fa7aa1447b7b0507fd907ec38af.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/7422c5a63c7847edb08b5021b1a1e1bd.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/55df318fe0cb46119fe7735657a53f9e.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/4c55acf197334a978b884e814337333f.png" alt="6.jpg"></p>
<blockquote>
<p>练习题代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">list1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line">list2 = []</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;=========通过for循环===============\n&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> list1:</span><br><span class="line">    <span class="keyword">if</span> element % <span class="number">2</span> == <span class="number">0</span>:</span><br><span class="line">        list2.append(element)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;for循环偶数：<span class="subst">&#123;list2&#125;</span>  \n&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;========通过while循环 ==========\n&quot;</span>)</span><br><span class="line">i = <span class="number">0</span></span><br><span class="line">list3 = []</span><br><span class="line"><span class="keyword">while</span> i &lt; <span class="built_in">len</span>(list1):</span><br><span class="line">    <span class="keyword">if</span> list1[i] % <span class="number">2</span> == <span class="number">0</span>:</span><br><span class="line">        list3.append(list1[i])</span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;while循环结果：<span class="subst">&#123;list3&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="数据容器：tuple-元组"><a href="#数据容器：tuple-元组" class="headerlink" title="数据容器：tuple(元组)"></a><strong>数据容器：tuple(元组)</strong></h2><p><img src="https://img-blog.csdnimg.cn/eb316d5f93874313a7eab587c27f8b8a.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/7ca934168ef44bd9a7e6c746a609d235.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/c0246230bf044c16ad2e1a74d2840b60.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/a0d232cd01f24efe88bda52bd1e5f1d5.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/451c988ba75742a8be876c9aedf29e53.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/5573b530e44c41fc8dac13032cb15195.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/faff9d47bd2340768948b1135a347364.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/5fe7f841243d44c0958d56b559a9a0b4.png" alt="8.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">students = (<span class="string">&#x27;周杰伦&#x27;</span>, <span class="number">11</span>, [<span class="string">&quot;foot&quot;</span>, <span class="string">&quot;music&quot;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查询年龄所在的下标</span></span><br><span class="line">index  = students.index(<span class="number">11</span>)</span><br><span class="line"><span class="built_in">print</span>(index)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查询学生姓名</span></span><br><span class="line">name = students[<span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(name)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除学生爱好</span></span><br><span class="line"><span class="keyword">del</span> students[<span class="number">2</span>][<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(students)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 增加爱好 coding 到 爱好list内</span></span><br><span class="line">students[<span class="number">2</span>].append(<span class="string">&quot;coding&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(students)</span><br></pre></td></tr></table></figure>
<h2 id="数据容器：str-字符串"><a href="#数据容器：str-字符串" class="headerlink" title="数据容器：str(字符串)"></a><strong>数据容器：str(字符串)</strong></h2><p><img src="https://img-blog.csdnimg.cn/793d5dd0e96e4bb3a6fdbbeb897b99b4.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/9fcc310a8c9e4b2c9cd4f7fad3510d81.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/24e1822f2a5c4cb8b55592bef6809d48.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/53c13ec6595846a881f7ce95e819e36b.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/1b23921df3fb41aeb17a7ee99ba70466.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/ba6c5d2488cf4288aeeb9d8c372dee22.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/1a272d79741d41b592ad12cf69b1eb18.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/0d8d1fa2495f425aa12a9e7d20518171.png" alt="8.jpg"><br><img src="https://img-blog.csdnimg.cn/c3d80805c0a74fc0a421f54fe6b02f0a.png" alt="9.jpg"><br><img src="https://img-blog.csdnimg.cn/a19bd5047a1340cbb8507cdbb2359901.png" alt="10.jpg"><br><img src="https://img-blog.csdnimg.cn/10062cd67678453a93a0390799e1ffd8.png" alt="11.jpg"><br><img src="https://img-blog.csdnimg.cn/09a0d98350314243abc169277079fa2f.png" alt="12.jpg"></p>
<blockquote>
<p>字符串的常用操作演示代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">my_str= <span class="string">&#x27; 12sdasd adfssdfsfswer &#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过下标索引取值</span></span><br><span class="line">v = my_str[<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(v)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查询字符所在索引下标</span></span><br><span class="line">index = my_str.index(<span class="string">&#x27;e&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(index)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 替换</span></span><br><span class="line">new_str = my_str.replace(<span class="string">&#x27;adf&#x27;</span>,<span class="string">&#x27;您好&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(new_str)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 分割</span></span><br><span class="line">lists = my_str.split(<span class="string">&#x27; &#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(lists)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 不填写参数则去除首尾空格,如果填写参数，则去除参数</span></span><br><span class="line">new_str_strip = my_str.strip()</span><br><span class="line"><span class="built_in">print</span>(new_str_strip)</span><br><span class="line"><span class="built_in">print</span>( my_str.strip(<span class="string">&#x27;2&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 统计字符串出现的次数</span></span><br><span class="line"><span class="built_in">print</span>( my_str.count(<span class="string">&#x27;a&#x27;</span>) )</span><br><span class="line"></span><br><span class="line"><span class="comment"># 统计字符串的长度</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(my_str ))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串遍历</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> my_str:</span><br><span class="line">    <span class="built_in">print</span>(x)</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/82252c728c074a74b206d8b0d3e8d545.png" alt="13.jpg"><br><img src="https://img-blog.csdnimg.cn/5fae72db41d24255b3c51b67259f20dd.png" alt="14.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;======================  练习题  ======================&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">练习题：</span></span><br><span class="line"><span class="string">给定一个字符串: &quot;litheima itcast boxuegul</span></span><br><span class="line"><span class="string">●统计字符串内有多少个&quot;it&quot;字符</span></span><br><span class="line"><span class="string">●将字符串内的空格，全部替换为字符: &quot;|&quot;</span></span><br><span class="line"><span class="string">●并按照&quot;|&quot;进行字符串分割，得到列表</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">practise = <span class="string">&quot;litheima itcast boxuegul&quot;</span></span><br><span class="line">count = practise.count(<span class="string">&#x27;it&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;统计字符串内有多少个<span class="subst">&#123;count&#125;</span> it 字符&quot;</span>)</span><br><span class="line"></span><br><span class="line">new_prac = practise.replace(<span class="string">&#x27; &#x27;</span>,<span class="string">&quot;|&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;新的字符串：<span class="subst">&#123;new_prac&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">lists = new_prac.split(<span class="string">&quot;|&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;按照 | 进行字符串分割，得到列表<span class="subst">&#123;lists&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="数据容器（序列）的切片"><a href="#数据容器（序列）的切片" class="headerlink" title="数据容器（序列）的切片"></a><strong>数据容器（序列）的切片</strong></h2><p><img src="https://img-blog.csdnimg.cn/10ef83e5aa9b40ec93169bb02e9573b6.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/42a84c215eaa41a08d217337b5f7fb0f.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/5b03ed6a88f94e7582be6d0ce1097b59.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/1912cf23616545da9142e237de05a155.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/003b04366a14424aab20aec6324d2df5.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/e803d81e58ca457492a69a1ca847ddeb.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/1e0ae0b3ef764c4595ec0ac4142f5deb.png" alt="7.jpg"></p>
<blockquote>
<p>代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">序列的切片操作</span></span><br><span class="line"><span class="string">序列</span></span><br><span class="line"><span class="string">序列是指:内容连续、有序，可使用下标索引的一类数据容器</span></span><br><span class="line"><span class="string">列表、元组、字符串，均可以可以视为序列。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">序列支持切片，即:列表、元组、字符串，均支持进行切片操作</span></span><br><span class="line"><span class="string">切片:从一个序列中，取出一个子序列</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">语法:序列[起始下标:结束下标:步长]</span></span><br><span class="line"><span class="string">表示从序列中，从指定位置开始，依次取出元素，到指定位置结束，得到一个新序列:</span></span><br><span class="line"><span class="string">    ●起始下标表示从何处开始，可以留空，留空视作从头开始</span></span><br><span class="line"><span class="string">    ●结束下标(不含)表示何处结束，可以留空，留空视作截取到结尾</span></span><br><span class="line"><span class="string">步长表示，依次取元素的间隔</span></span><br><span class="line"><span class="string">    ●步长1表示，-个个取元素</span></span><br><span class="line"><span class="string">    ●步长2表示，每次跳过1个元素取</span></span><br><span class="line"><span class="string">    ●步长N表示，每次跳过N-1个元素取</span></span><br><span class="line"><span class="string">    ●步长为负数表示，反向取(注意，起始下标和结束下标也要反向标记)</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 对list进行切片，从1开始，4结束，步长1</span></span><br><span class="line">mylist = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line">res1 = mylist[<span class="number">1</span>:<span class="number">4</span>]</span><br><span class="line"><span class="built_in">print</span>(res1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对tuple进行切片，从头开始，到最后结束，步长1</span></span><br><span class="line">t1 = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>)</span><br><span class="line"><span class="built_in">print</span>(t1[:])</span><br><span class="line"><span class="comment"># 对str进行切片，从头开始，到最后结束，步长2</span></span><br><span class="line"></span><br><span class="line">str1 = <span class="string">&quot;123456789&quot;</span></span><br><span class="line"><span class="built_in">print</span>(str1[::<span class="number">2</span>])</span><br><span class="line"><span class="comment"># 对str进行切片，从头开始，到最后结束，步长-1</span></span><br><span class="line"><span class="built_in">print</span>(str1[::-<span class="number">1</span>])  <span class="comment"># 等同于将序列翻转</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 对列表进行切片，从3开始，到1结束，步长-1</span></span><br><span class="line">mylist = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line">nets = mylist[<span class="number">3</span>:<span class="number">1</span>:-<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(nets)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对元组进行切片，从头开始，到尾结束，步长-2</span></span><br><span class="line">t1 = (<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>)</span><br><span class="line">new_t1 = t1[::-<span class="number">2</span>]</span><br><span class="line"><span class="built_in">print</span>(new_t1)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;=======================练习题 =======================&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">练习案例:序列的切片实践</span></span><br><span class="line"><span class="string">有字符串: &quot;万过薪月，员序程马黑来，nohtyP学&quot;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">●请使用学过的任何方式，得到&quot;黑马程序员&quot;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">可用方式参考:</span></span><br><span class="line"><span class="string">●倒序字符串，切片取出或切片取出，然后倒序 </span></span><br><span class="line"><span class="string">●split分隔&quot;，&quot; replace替换&quot;来&quot;为空，倒序字符串</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">str2 = <span class="string">&quot;万过薪月，员序程马黑来，nohtyP学&quot;</span></span><br><span class="line"><span class="comment"># 请使用学过的任何方式，得到&quot;黑马程序员&quot;</span></span><br><span class="line"><span class="comment"># 方法1 ；倒序字符串，切片取出</span></span><br><span class="line">news = str2[::-<span class="number">1</span>][<span class="number">9</span>:<span class="number">14</span>]</span><br><span class="line"><span class="built_in">print</span>(news)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法2 ：切片取出，然后倒序</span></span><br><span class="line">news = str2[<span class="number">5</span>:<span class="number">10</span>][::-<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(news) </span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法3： split分隔&quot;，&quot; replace替换&quot;来&quot;为空，倒序字符串</span></span><br><span class="line">news = str2.split(<span class="string">&quot;，&quot;</span>)[<span class="number">1</span>].replace(<span class="string">&quot;来&quot;</span>,<span class="string">&#x27;&#x27;</span>)[::-<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(news)</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="数据容器：set-集合"><a href="#数据容器：set-集合" class="headerlink" title="数据容器：set(集合)"></a><strong>数据容器：set(集合)</strong></h2><p><img src="https://img-blog.csdnimg.cn/8f6551dd2cd948ce92e92e863bf3396b.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/eaf3c752afc14a17a2677af69a6fcd61.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/5a248accfc2d4195a1fd7db0a51dd132.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/d00c577e15c84a6588b4f725470e14cc.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/57b6a9be649544a4b097a0dbba8014a6.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/38cbbda427fa4c9699782c865fb07fca.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/b1e603048a03495aaa03a3dcc27c1c38.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/97121a16288e49cf81c9d131e528c3c5.png" alt="8.jpg"><br><img src="https://img-blog.csdnimg.cn/ec1bf079725c4331b765202ceffc3fec.png" alt="9.jpg"><br><img src="https://img-blog.csdnimg.cn/3503d18a09e9469b8128b49d416668df.png" alt="10.jpg"><br><img src="https://img-blog.csdnimg.cn/0efb5a1efe324db0826c5906252bc5c5.png" alt="11.jpg"><br><img src="https://img-blog.csdnimg.cn/451c760b0be04d79a00606d65cc7f690.png" alt="12.jpg"><br><img src="https://img-blog.csdnimg.cn/ebffea09b9dd4718962ac93d78524dc9.png" alt="13.jpg"><br><img src="https://img-blog.csdnimg.cn/26aa273fa41849d48c281ffb1e39be64.png" alt="14.jpg"><br><img src="https://img-blog.csdnimg.cn/1d44312ec4254a4ab1b8a6fd6b1130cf.png" alt="15.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">数据容器：set ( 集合 )</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">◎学习目标</span></span><br><span class="line"><span class="string">    1.掌握集合的定义格式</span></span><br><span class="line"><span class="string">    2.掌握集合的特点</span></span><br><span class="line"><span class="string">    3.掌握集合的常见操作</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">集合 的特点：</span></span><br><span class="line"><span class="string">    ● 不支持元素的重复</span></span><br><span class="line"><span class="string">    ● 内容是无序的</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">集合使用： &#123;&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">集合基础语法：</span></span><br><span class="line"><span class="string">    # 定义集合字面量</span></span><br><span class="line"><span class="string">    &#123;元素1，元素2，元素3，.....&#125;</span></span><br><span class="line"><span class="string">    # 定义集合变量</span></span><br><span class="line"><span class="string">    变量名称 = &#123; 元素1，元素2，...... &#125;</span></span><br><span class="line"><span class="string">    # 定义空集合</span></span><br><span class="line"><span class="string">    变量名称 = set()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">集合 不支持 下标访问</span></span><br><span class="line"><span class="string">集合常用操作：</span></span><br><span class="line"><span class="string">● 添加新元素</span></span><br><span class="line"><span class="string">    语法：集合.add(元素)</span></span><br><span class="line"><span class="string">    结果：集合本身被修改，添加了新元素</span></span><br><span class="line"><span class="string">● 移除元素</span></span><br><span class="line"><span class="string">    语法：集合.remove(元素)</span></span><br><span class="line"><span class="string">    结果，集合本身被修改，移除了元素</span></span><br><span class="line"><span class="string">● 随机取出来一个元素</span></span><br><span class="line"><span class="string">    语法：集合.pop()</span></span><br><span class="line"><span class="string">    结果：会得到一个元素的结果，同时集合本身被修改，取出来的这个元素被移除</span></span><br><span class="line"><span class="string">● 清空集合</span></span><br><span class="line"><span class="string">    语法：集合.clear()</span></span><br><span class="line"><span class="string">    结果：集合清空，变为空集合</span></span><br><span class="line"><span class="string">● 取出2个集合的差集</span></span><br><span class="line"><span class="string">    语法：集合1.difference(集合2) 功能：取出集合1和集合2的差集 ( 简单理解：集合1有而集合2 没有的 )</span></span><br><span class="line"><span class="string">    结果：得到一个新集合，集合1和集合2不变</span></span><br><span class="line"><span class="string">● 消除2个集合的差集</span></span><br><span class="line"><span class="string">    语法：集合1.difference_update(集合2)</span></span><br><span class="line"><span class="string">    功能：对比集合1和集合2，在集合1内，删除和集合2相同的元素</span></span><br><span class="line"><span class="string">    结果：集合1被修改，集合2不变</span></span><br><span class="line"><span class="string">● 2个集合合并</span></span><br><span class="line"><span class="string">    语法：集合1.union(集合2)</span></span><br><span class="line"><span class="string">    功能：将集合1和集合2组合成一个新的集合</span></span><br><span class="line"><span class="string">    结果：得到新集合，集合1和集合2不变</span></span><br><span class="line"><span class="string">● 统计集合元素数量</span></span><br><span class="line"><span class="string">    语法：len(集合)</span></span><br><span class="line"><span class="string">    结果：返回数量</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义集合 (集合会自动去除重复,且顺序是无法保证的 无序的 随机顺序 ，不支持下标访问)</span></span><br><span class="line">my_set = &#123;<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;1&quot;</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(my_set)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(my_set))</span><br><span class="line"><span class="comment"># 添加新元素</span></span><br><span class="line">my_set.add(<span class="string">&quot;黑马&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;my_set添加元素后，结果为:<span class="subst">&#123;my_set&#125;</span>&quot;</span>)</span><br><span class="line"><span class="comment"># 移除元素</span></span><br><span class="line">my_set.remove(<span class="string">&quot;黑马&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(my_set)</span><br><span class="line"><span class="comment"># 随机取出一个元素</span></span><br><span class="line">element = my_set.pop()</span><br><span class="line"><span class="built_in">print</span>(element)</span><br><span class="line"><span class="built_in">print</span>(my_set)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 清空集合</span></span><br><span class="line">my_set.clear()</span><br><span class="line"><span class="built_in">print</span>(my_set)</span><br><span class="line"><span class="comment"># 取2个集合的差集</span></span><br><span class="line">set1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;</span><br><span class="line">set2 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;</span><br><span class="line">set3 = set1.difference(set2)</span><br><span class="line"><span class="built_in">print</span>(set3)</span><br><span class="line"><span class="comment"># 消除2个集合的差集</span></span><br><span class="line">set1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;</span><br><span class="line">set2 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;</span><br><span class="line">set1.difference_update(set2)</span><br><span class="line"><span class="built_in">print</span>(set1)</span><br><span class="line"><span class="built_in">print</span>(set2)</span><br><span class="line"><span class="comment"># 2个集合合并为1个</span></span><br><span class="line">set1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;</span><br><span class="line">set2 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;</span><br><span class="line">set4 = set1.union(set2)</span><br><span class="line"><span class="built_in">print</span>(set4)</span><br><span class="line"><span class="comment"># 统计集合元素数量</span></span><br><span class="line">set2 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(set2))</span><br><span class="line"><span class="comment"># 集合遍历</span></span><br><span class="line"><span class="comment"># 集合不支持下标索引，所以不能用while循环遍历</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> set2:</span><br><span class="line">    <span class="built_in">print</span>(x)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;================================= 练习题 =================================&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">信息去重</span></span><br><span class="line"><span class="string">有如下列表对象:</span></span><br><span class="line"><span class="string">my_list= [&#x27;黑马程序员&#x27;, &#x27;传智播客&#x27;, &#x27;黑马程序员&#x27;, &#x27;传智播客&#x27;, &#x27;itheima&#x27;, &#x27;itcast&#x27;, &#x27;itheima&#x27;, &#x27;itcast&#x27;,</span></span><br><span class="line"><span class="string">&#x27;best&#x27;]</span></span><br><span class="line"><span class="string">请:</span></span><br><span class="line"><span class="string">题目要求：</span></span><br><span class="line"><span class="string">● 定义一个空集合</span></span><br><span class="line"><span class="string">● 通过for循环遍历列表,在for循环中将列表的元素添加至集合</span></span><br><span class="line"><span class="string">● 最终得到元素去重后的集合对象，并打印输出</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">my_list= [<span class="string">&#x27;黑马程序员&#x27;</span>, <span class="string">&#x27;传智播客&#x27;</span>, <span class="string">&#x27;黑马程序员&#x27;</span>, <span class="string">&#x27;传智播客&#x27;</span>, <span class="string">&#x27;itheima&#x27;</span>, <span class="string">&#x27;itcast&#x27;</span>, <span class="string">&#x27;itheima&#x27;</span>, <span class="string">&#x27;itcast&#x27;</span>,</span><br><span class="line"><span class="string">&#x27;best&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个空集合</span></span><br><span class="line">my_set = <span class="built_in">set</span>()</span><br><span class="line"><span class="comment"># 通过for循环遍历列表,在for循环中将列表的元素添加至集合</span></span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> my_list:</span><br><span class="line">    my_set.add(element)</span><br><span class="line"><span class="comment"># 最终得到元素去重后的集合对象，并打印输出</span></span><br><span class="line"><span class="built_in">print</span>(my_set)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="数据容器：dict-字典、映射"><a href="#数据容器：dict-字典、映射" class="headerlink" title="数据容器：dict(字典、映射)"></a><strong>数据容器：dict(字典、映射)</strong></h2><ul>
<li>字典的定义</li>
<li>字典的常用操作</li>
</ul>
<blockquote>
<p><strong>字典的定义</strong></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/77c41ac32c194ddab0df9f3732ed313f.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/0d42e3a5ce8a4bb5a1b8ed34cbab1fe8.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/d193b17913c14f158b343b4fb3610506.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/7353460e1f074f1d8e935bee0f858227.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/8940310346bd469ba1853c2c4ad8002c.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/bbaa44d138ee4623989ea9b13b7b14f2.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/1c47a443de5c4a04a915ea23dfbd94e7.png" alt="7.jpg"></p>
<blockquote>
<p><strong>字典的常用操作</strong></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/92d33048600141e2b9ad7e1b2e323819.jpeg" alt="1"><br><img src="https://img-blog.csdnimg.cn/e77c87041009443babc30c0f66a4812c.jpeg" alt="2"><br><img src="https://img-blog.csdnimg.cn/0f510edfeb99493fa7ba17bc4deb9e64.jpeg" alt="3"><br><img src="https://img-blog.csdnimg.cn/a2a99accf94c4b3e97a4c77bc3ba2c85.jpeg" alt="4"><br><img src="https://img-blog.csdnimg.cn/527c32dbd5df46f6a8441a087292639a.jpeg" alt="5"><br><img src="https://img-blog.csdnimg.cn/499c66e2bcef4ad4b4c99d57bfc5fec5.jpeg" alt="6"><br><img src="https://img-blog.csdnimg.cn/90ecd1e5a7914b3fb669291bd718030d.jpeg" alt="7"><br><img src="https://img-blog.csdnimg.cn/6d45602e1b9c48d0bb2e7f904d3be5b4.jpeg" alt="8"><br><img src="https://img-blog.csdnimg.cn/36e6cd0e1e3c4a2f805d14212ff9d3cc.jpeg" alt="9"></p>
<blockquote>
<p>代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">数据容器：字典 ( dict ，映射)</span></span><br><span class="line"><span class="string">学习目标：</span></span><br><span class="line"><span class="string">    1.字典的定义</span></span><br><span class="line"><span class="string">    2.字典的常用操作</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">1）字典的基础定义</span></span><br><span class="line"><span class="string">    可以通过key 取到对应的value ( 注意： key不可以重复 重复添加等同于覆盖原始数据)</span></span><br><span class="line"><span class="string">    使用 &#123;&#125; , 不过存储的元素是一个个的 键值对</span></span><br><span class="line"><span class="string">    语法：</span></span><br><span class="line"><span class="string">        # 定义字典字面量</span></span><br><span class="line"><span class="string">        &#123;key:value, key1:value1 , key2:value2 , .....&#125;</span></span><br><span class="line"><span class="string">        # 定义字典变量</span></span><br><span class="line"><span class="string">        my_dict = &#123;key:value, key1:value1 , key2:value2 , .....&#125;</span></span><br><span class="line"><span class="string">        # 定义空字典</span></span><br><span class="line"><span class="string">        my_dict = &#123;&#125;</span></span><br><span class="line"><span class="string">        my_dict = dict()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">字典的常用操作：</span></span><br><span class="line"><span class="string">    ● 新增元素</span></span><br><span class="line"><span class="string">        语法：字典[key] = value</span></span><br><span class="line"><span class="string">        结果；字典被修改，新增了元素</span></span><br><span class="line"><span class="string">    ● 更新元素</span></span><br><span class="line"><span class="string">        语法：字典[key] = value</span></span><br><span class="line"><span class="string">        结果；字典被修改，元素被修改</span></span><br><span class="line"><span class="string">        注意：字典key不可以重复，如果key已经存在，则为更新，不存在则为新增</span></span><br><span class="line"><span class="string">    ● 删除元素</span></span><br><span class="line"><span class="string">        语法：字典.pop(key)</span></span><br><span class="line"><span class="string">        结果：获取指定key的value,同时字典被修改，指定key的数据被删除</span></span><br><span class="line"><span class="string">    ● 清空字典</span></span><br><span class="line"><span class="string">        语法：字典.clear()</span></span><br><span class="line"><span class="string">        结果:字都被修改，元素被清空</span></span><br><span class="line"><span class="string">    ● 获取全部的key</span></span><br><span class="line"><span class="string">        语法：字典.keys()</span></span><br><span class="line"><span class="string">        结果：得到字典中的全部key</span></span><br><span class="line"><span class="string">    ● 统计字典的元素数量</span></span><br><span class="line"><span class="string">        语法：len(字典)</span></span><br><span class="line"><span class="string">        结果：返回字典内的元素数量</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义字典</span></span><br><span class="line">my_dict = &#123;<span class="string">&quot;王力宏&quot;</span>: <span class="string">&quot;99分&quot;</span>, <span class="string">&quot;周杰伦&quot;</span>: <span class="string">&quot;100分&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义空字典</span></span><br><span class="line">my_dict = &#123;&#125;</span><br><span class="line">my_dict = <span class="built_in">dict</span>()</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(my_dict)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(my_dict))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义重复key的字典(  字典不重复 会自动去除重复的key ，重复添加等同于覆盖原始数据)</span></span><br><span class="line">my_dict = &#123;<span class="string">&quot;王力宏&quot;</span>: <span class="string">&quot;99分&quot;</span>, <span class="string">&quot;周杰伦&quot;</span>: <span class="string">&quot;100分&quot;</span>, <span class="string">&quot;王力宏&quot;</span>: <span class="string">&quot;991分&quot;</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(my_dict)</span><br><span class="line"><span class="comment"># 通过字典key 取出对应的value</span></span><br><span class="line">stc = my_dict[<span class="string">&quot;王力宏&quot;</span>]</span><br><span class="line"><span class="built_in">print</span>(stc)</span><br><span class="line"><span class="comment"># 定义嵌套字典</span></span><br><span class="line">banji = &#123;</span><br><span class="line">    <span class="string">&quot;王力宏&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;语文&quot;</span>: <span class="string">&quot;100分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;数学&quot;</span>: <span class="string">&quot;90分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;英语&quot;</span>: <span class="string">&quot;80分&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;周杰伦&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;语文&quot;</span>: <span class="string">&quot;75分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;数学&quot;</span>: <span class="string">&quot;66分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;英语&quot;</span>: <span class="string">&quot;58分&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;林俊杰&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;语文&quot;</span>: <span class="string">&quot;63分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;数学&quot;</span>: <span class="string">&quot;23分&quot;</span>,</span><br><span class="line">        <span class="string">&quot;英语&quot;</span>: <span class="string">&quot;45分&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">print</span>(banji)</span><br><span class="line"><span class="comment"># 从嵌套字典中获取数据</span></span><br><span class="line"><span class="comment"># 比如： 周杰伦的信息</span></span><br><span class="line"><span class="built_in">print</span>(banji[<span class="string">&#x27;周杰伦&#x27;</span>][<span class="string">&quot;语文&quot;</span>])</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;======================== 字典的常用操作 ========================&quot;</span>)</span><br><span class="line"><span class="comment"># 新增</span></span><br><span class="line">d1 = &#123;<span class="string">&quot;王力宏&quot;</span>: <span class="string">&quot;99分&quot;</span>, <span class="string">&quot;周杰伦&quot;</span>: <span class="string">&quot;100分&quot;</span>&#125;</span><br><span class="line">d1[<span class="string">&quot;张信哲&quot;</span>] = <span class="string">&quot;120分&quot;</span></span><br><span class="line"><span class="built_in">print</span>(d1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 更新</span></span><br><span class="line">d1[<span class="string">&#x27;周杰伦&#x27;</span>] = <span class="string">&quot;23分&quot;</span></span><br><span class="line"><span class="built_in">print</span>(d1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除元素</span></span><br><span class="line">sc1 = d1.pop(<span class="string">&quot;王力宏&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;字典中被移除一个元素，结果为:<span class="subst">&#123;d1&#125;</span>.王力宏的分数为<span class="subst">&#123;sc1&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 清空元素</span></span><br><span class="line">d1.clear()</span><br><span class="line"><span class="built_in">print</span>(d1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取全部的key</span></span><br><span class="line">d1 = &#123;<span class="string">&quot;王力宏&quot;</span>: <span class="string">&quot;99分&quot;</span>, <span class="string">&quot;周杰伦&quot;</span>: <span class="string">&quot;100分&quot;</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(d1.keys())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历字典</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> d1:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot; <span class="subst">&#123;x&#125;</span> =&gt; <span class="subst">&#123;d1[x]&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典不支持while 循环</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 统计字典的元素数量</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(d1))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;======================== 字典的练习题 ========================&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">升职加薪</span></span><br><span class="line"><span class="string">有如下员工信息，请使用字典完成数据的记录。</span></span><br><span class="line"><span class="string">并通过for循环，对所有级别为1级的员工，级别上升1级，薪水增加1000元</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">Staff = &#123;</span><br><span class="line">    <span class="string">&quot;张三&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;部分&quot;</span>:<span class="string">&quot;科技部&quot;</span>,</span><br><span class="line">        <span class="string">&quot;工资&quot;</span>:<span class="number">3000</span>,</span><br><span class="line">        <span class="string">&quot;级别&quot;</span>:<span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;李四&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;部分&quot;</span>:<span class="string">&quot;人资部&quot;</span>,</span><br><span class="line">        <span class="string">&quot;工资&quot;</span>:<span class="number">5000</span>,</span><br><span class="line">        <span class="string">&quot;级别&quot;</span>:<span class="number">2</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;王五&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;部分&quot;</span>:<span class="string">&quot;市场部&quot;</span>,</span><br><span class="line">        <span class="string">&quot;工资&quot;</span>:<span class="number">1000</span>,</span><br><span class="line">        <span class="string">&quot;级别&quot;</span>:<span class="number">3</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过for循环，对所有级别为1级的员工，级别上升1级，薪水增加1000元</span></span><br><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> Staff:</span><br><span class="line">    <span class="keyword">if</span> Staff[name][<span class="string">&quot;级别&quot;</span>] == <span class="number">1</span>:</span><br><span class="line">        <span class="comment"># 级别上升1级</span></span><br><span class="line">        Staff[name][<span class="string">&quot;级别&quot;</span>] += <span class="number">1</span></span><br><span class="line">        <span class="comment"># 薪水增加1000元</span></span><br><span class="line">        Staff[name][<span class="string">&#x27;工资&#x27;</span>] += <span class="number">1000</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(Staff)</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/3456b572564c4f4fa92877fbdcf01f65.jpeg"><br><img src="https://img-blog.csdnimg.cn/1a2ab88e29db427988afd2150e795c86.jpeg"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字典定义</span></span><br><span class="line">dict1 = <span class="built_in">dict</span>()</span><br><span class="line"></span><br><span class="line">student = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;tom&quot;</span>, <span class="string">&quot;age&quot;</span>: <span class="number">18</span>, <span class="string">&quot;love&quot;</span>: <span class="string">&quot;coding&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典的增加 ，如果下标不存在，则新加</span></span><br><span class="line">student[<span class="string">&#x27;web&#x27;</span>] = <span class="string">&#x27;aaa&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典的修改，如果下标存在，则修改，不存在则新增</span></span><br><span class="line">student[<span class="string">&#x27;web2&#x27;</span>] = <span class="string">&#x27;html+css+javascript技术&#x27;</span>  <span class="comment"># 下标不存在，新增</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(student)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典的删除</span></span><br><span class="line">student.pop(<span class="string">&#x27;web2&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(student)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询 1.使用键值查询</span></span><br><span class="line"></span><br><span class="line">name = student[<span class="string">&#x27;name&#x27;</span>]</span><br><span class="line"><span class="built_in">print</span>(name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询 2.利用get函数使用键查找值，如果key不存在返回None</span></span><br><span class="line">name1 = student.get(<span class="string">&#x27;name&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(name1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询 3.利用setdefault函数查询，和get函数差不多，但是如果键不存在，则查找的内容当做键放入字典，并设置默认值，不设置为None</span></span><br><span class="line">dd = student.setdefault(<span class="string">&#x27;xinQing&#x27;</span>, <span class="string">&quot;每天都是好心情&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(dd)</span><br><span class="line"><span class="built_in">print</span>(student)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询所有所有key   keys()  # 获取所有的键,返回列表</span></span><br><span class="line">allKey = student.keys()</span><br><span class="line"><span class="built_in">print</span>(allKey)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询所有value   values()  # 获取所有的值,返回列表</span></span><br><span class="line">allValue = student.values()</span><br><span class="line"><span class="built_in">print</span>(allValue)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典查询所有键值对  items()   # 获取所有键值对,返回元祖组成的列表</span></span><br><span class="line">allDict = student.items()</span><br><span class="line"><span class="built_in">print</span>(allDict)</span><br><span class="line"></span><br><span class="line"><span class="comment"># dict_keys()，dict_values()，dict_items()，这样的数据是没有办法按照列表下标进行访问的，可以转成列表或者元祖后再访问</span></span><br><span class="line">allList = <span class="built_in">list</span>(allDict)</span><br><span class="line"><span class="built_in">print</span>(allList[<span class="number">0</span>][<span class="number">1</span>])  <span class="comment"># 例如 查询name的值 tom</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="数据容器的通用操作"><a href="#数据容器的通用操作" class="headerlink" title="数据容器的通用操作"></a><strong>数据容器的通用操作</strong></h2><p><img src="https://img-blog.csdnimg.cn/a84ef6170808421e9ff47d89960b631e.jpeg" alt="0"><br><img src="https://img-blog.csdnimg.cn/d792c14ffabc477985751cc9acade77b.jpeg" alt="1"><br><img src="https://img-blog.csdnimg.cn/a16f81eb57dd42dabb21193f1258c5ec.jpeg" alt="2"><br><img src="https://img-blog.csdnimg.cn/e37a85701fdb42e286fd9c50f37147b4.jpeg" alt="3"><br><img src="https://img-blog.csdnimg.cn/bcf8d66e7d8c48a3a7fde94d815a2468.jpeg" alt="4"><br><img src="https://img-blog.csdnimg.cn/22155bc5e3534ba39258aaf510b43cea.jpeg" alt="5"><br><img src="https://img-blog.csdnimg.cn/8b7548cf0bc34f0587abf6ef7c0e3b88.jpeg" alt="6"><br><img src="https://img-blog.csdnimg.cn/bd2cd18bf2eb461e98338ff2bceec890.jpeg" alt="7"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">数据容器的通用操作</span></span><br><span class="line"><span class="string">    1：遍历</span></span><br><span class="line"><span class="string">        ● for 循环都支持</span></span><br><span class="line"><span class="string">        ● 列表，元组，字符串 支持while循环，集合、字典不支持(无法下标索引)</span></span><br><span class="line"><span class="string">    2.统计功能</span></span><br><span class="line"><span class="string">        ● len(容器) : 统计预算数量</span></span><br><span class="line"><span class="string">        ● min(容器) : 最小的元素</span></span><br><span class="line"><span class="string">        ● max(容器) : 最大的元素</span></span><br><span class="line"><span class="string">    3.转换功能</span></span><br><span class="line"><span class="string">        ● list(容器) : 将给定容器转换为列表</span></span><br><span class="line"><span class="string">        ● str(容器)  : 将给定容器转换为字符串</span></span><br><span class="line"><span class="string">        ● tuple(容器): 将给定容器转换为元组</span></span><br><span class="line"><span class="string">        ● set(容器)  : 将给定容器转换为集合</span></span><br><span class="line"><span class="string">    4.排序功能</span></span><br><span class="line"><span class="string">        ● sorted(容器) ： 将给定容器进行排序 从小到大</span></span><br><span class="line"><span class="string">        ● sorted(容器，reverse = True) : 排序反转 ( 即 降序排序)</span></span><br><span class="line"><span class="string">        结果：排序后的结果为列表</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz">王扣</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz/posts/24e420f7.html">http://grace.666443.xyz/posts/24e420f7.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://grace.666443.xyz" target="_blank">王扣笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/f284fb2dc534d3975a7733ab0eb843eb.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/b515868e.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/2be5cfbaf7fdc330fbcdbc43704e5405.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">08.Python函数进阶</div></div></a></div><div class="next-post pull-right"><a href="/posts/b2d84ecf.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">06.Python函数</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王扣</div><div class="author-info__description">常用开发中的笔记记录</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%85%A5%E9%97%A8%E8%AF%BE%E7%A8%8B-%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8"><span class="toc-number">1.</span> <span class="toc-text">Python入门课程  数据容器</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E7%9B%AE%E5%BD%95"><span class="toc-number">1.1.</span> <span class="toc-text">章节目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%E5%85%A5%E9%97%A8"><span class="toc-number">1.2.</span> <span class="toc-text">数据容器入门</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%9Alist-%E5%88%97%E8%A1%A8"><span class="toc-number">1.3.</span> <span class="toc-text">数据容器：list(列表)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E2%91%A0%E5%88%97%E8%A1%A8%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.1.</span> <span class="toc-text">①列表的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E2%91%A1%E5%88%97%E8%A1%A8%E7%9A%84%E4%B8%8B%E6%A0%87%E7%B4%A2%E5%BC%95"><span class="toc-number">1.3.2.</span> <span class="toc-text">②列表的下标索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E2%91%A2%E5%88%97%E8%A1%A8%E7%9A%84%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">1.3.3.</span> <span class="toc-text">③列表的常用操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#list%EF%BC%88%E5%88%97%E8%A1%A8%EF%BC%89%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-number">1.4.</span> <span class="toc-text">list（列表）的遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%9Atuple-%E5%85%83%E7%BB%84"><span class="toc-number">1.5.</span> <span class="toc-text">数据容器：tuple(元组)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%9Astr-%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.6.</span> <span class="toc-text">数据容器：str(字符串)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%88%E5%BA%8F%E5%88%97%EF%BC%89%E7%9A%84%E5%88%87%E7%89%87"><span class="toc-number">1.7.</span> <span class="toc-text">数据容器（序列）的切片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%9Aset-%E9%9B%86%E5%90%88"><span class="toc-number">1.8.</span> <span class="toc-text">数据容器：set(集合)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%EF%BC%9Adict-%E5%AD%97%E5%85%B8%E3%80%81%E6%98%A0%E5%B0%84"><span class="toc-number">1.9.</span> <span class="toc-text">数据容器：dict(字典、映射)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%AE%B9%E5%99%A8%E7%9A%84%E9%80%9A%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">1.10.</span> <span class="toc-text">数据容器的通用操作</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块"><img src="https://p.ananas.chaoxing.com/star3/origin/b230c5f9060b1f422a86e332d8e37926.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="18.Python爬虫-requests模块"/></a><div class="content"><a class="title" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块">18.Python爬虫-requests模块</a><time datetime="2022-12-08T12:53:30.000Z" title="发表于 2022-12-08 20:53:30">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取"><img src="https://p.ananas.chaoxing.com/star3/origin/8f2179d04bfe0e48cc706ae112c97952.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="17.Python爬虫-数据解析与提取"/></a><div class="content"><a class="title" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取">17.Python爬虫-数据解析与提取</a><time datetime="2022-12-08T12:52:55.000Z" title="发表于 2022-12-08 20:52:55">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5038ed.html" title="16.Python爬虫-基础"><img src="https://p.ananas.chaoxing.com/star3/origin/2054c03c91d338203c730610ead1b303.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="16.Python爬虫-基础"/></a><div class="content"><a class="title" href="/posts/5038ed.html" title="16.Python爬虫-基础">16.Python爬虫-基础</a><time datetime="2022-12-08T12:52:11.000Z" title="发表于 2022-12-08 20:52:11">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关"><img src="https://p.ananas.chaoxing.com/star3/origin/2a1821704670b5cf8c2289255e183d62.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="15.Python之Mysql数据库相关"/></a><div class="content"><a class="title" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关">15.Python之Mysql数据库相关</a><time datetime="2022-12-08T12:17:02.000Z" title="发表于 2022-12-08 20:17:02">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/6ebaa02b.html" title="14.Python面向对象"><img src="https://p.ananas.chaoxing.com/star3/origin/d24d9bfcd4ce3f910d6b5cdc78676d75.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="14.Python面向对象"/></a><div class="content"><a class="title" href="/posts/6ebaa02b.html" title="14.Python面向对象">14.Python面向对象</a><time datetime="2022-12-08T12:10:47.000Z" title="发表于 2022-12-08 20:10:47">2022-12-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 王扣</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><div class="fb-customerchat" id="fb-customer-chat" attribution="biz_inbox"></div><script>document.getElementById('fb-root') ? '' : document.body.insertAdjacentHTML('afterend', '<div id="fb-root"></div>')

window.fbAsyncInit = function() {
  FB.init({
    xfbml: true,
    version: 'v15.0'
  });
};

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/zh_CN/sdk/xfbml.customerchat.js';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

if (false) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      FB.CustomerChat.show();
    });
  }
  chatBtnFn()
} else {
  if (false) {
    function chatBtnHide () {
      FB.CustomerChat.hide()
    }
    function chatBtnShow () {
      FB.CustomerChat.show(false)
    }
  }
}</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>